#include "unp.h"

int main(int argc, char** argv){
	struct sockaddr_in	servaddr;
	int sockfd;

	if(argc != 2)
		err_quit("Usage: a.out <server ip>");

	if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
		err_sys("socket error");

	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(8888);
	if( inet_aton(argv[1], &servaddr.sin_addr) < 0)
		err_sys("inet_aton error");

	if( connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)
		err_sys("connect error");

	char sendbuf[MAXLINE];
	char recvbuf[MAXLINE];
	char *p = recvbuf;
	int n;
	while(1){
		fgets(sendbuf, sizeof(sendbuf), stdin);
		n = strlen(sendbuf) - 1;
		sendbuf[n] = 0;
		if( strncmp(sendbuf, "close", 5) == 0)
			break;
		write(sockfd, sendbuf, n);
		bzero(recvbuf, MAXLINE);
		n = read(sockfd, p, MAXLINE);
		if( n < 0)
			err_sys("read error");
		printf("%s\n", recvbuf);
	}
	return 0;
}
